import { runApp, IAppConfig, request } from 'ice';
import LocaleProvider from '@/components/LocaleProvider';
import Loading from './components/Loading';
import { getLocale } from '@/utils/locale';
import commonInterceptor from '@/utils/commonInterceptor';

const locale = getLocale();

const appConfig: IAppConfig = {
  app: {
    rootId: 'ice-container',
    addProvider: ({ children }) => <LocaleProvider locale={locale}>{children}</LocaleProvider>,
    getInitialData: async (ctx) => {
      const theme = localStorage.getItem('theme');
      let token = sessionStorage.getItem('token');
      const menus = [];
      if (!token) {
        token = localStorage.getItem('token');
      }
      // try {
      //   menus = await request.get('/erp/menu/pc', { headers: { Authorization: token } });
      //   console.debug(menus);
      // } catch (e) {
      //   console.warn(e);
      // }
      return { theme, token, menus };
    },
  },
  router: {
    type: 'browser',
    fallback: <Loading />,
  },
  request: [
    {
      baseURL: '/api',
      interceptors: commonInterceptor,
    },
  ],
};
runApp(appConfig);
